package com.thinkfit.jy.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.common.lib.activity.BaseActivity;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;
import com.thinkfit.jy.fragment.BluetoothFragment;
import com.thinkfit.jy.fragment.PeerListFragment;
import com.thinkfit.jy.fragment.SettingFragment;

import java.util.HashMap;

/**
 * 设置界面
 */
public class SettingActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.height = getDisplayMetrics().heightPixels;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
        int currentStyle = DataManager.Companion.getInstance().getMainStyle();
        params.gravity = Gravity.LEFT;
        if (currentStyle == 3) {
            Bitmap bmp = (Bitmap) DataManager.Companion.getInstance().getObject();
            Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, BaseUtils.StaticParams.dp2px(this, 320), getDisplayMetrics().heightPixels);
            setBackground(R.id.fl, newBmp);
        }
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setFinishOnTouchOutside(true);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fl, new SettingFragment());
        ft.commit();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            int to = bundle.getInt(Constants.BUNDLE_EXTRA, 0);
            if (to == 1) {
                toFragment(new BluetoothFragment());
            }else if(to == 2){
                toFragment(new PeerListFragment());
            }
        }
    }



    public void toFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fl, fragment);
        ft.addToBackStack(null);
        ft.commit();
    }


    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    public void onReceive(@NonNull HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if (map.containsKey(EventBusEvent.BLUETOOTH_SUCCESS)) {
            finish();
        } else {
            super.onReceive(map);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }

}